import datetime
import time
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from webdriver_manager.chrome import ChromeDriverManager

# 设置本地 Chrome 驱动路径
chrome_driver_path = "D:/webDriver/chromedriver-win64/chromedriver-win64/chromedriver.exe"

# 创建 WebDriver 对象
driver = webdriver.Chrome(service=Service(chrome_driver_path))

# 打开一个网页(https://www.baidu.com)
# driver.get("https://www.baidu.com")
# time.sleep(2)
# # 找到百度输入框，输入关键词“牢大”
# driver.find_element(By.CSS_SELECTOR,"#kw").send_keys("牢大")
# time.sleep(2)
# # 点击“百度一下”按钮
# driver.find_element(By.CSS_SELECTOR,"#su").click()
# time.sleep(2)

# 查找页面元素
# element=driver.find_element(By.CSS_SELECTOR,"#hotsearch-content-wrapper > li:nth-child(7)")
# 点击元素
# element.click()
# time.sleep(2)

# 查找多个元素
# elements=driver.find_elements(By.CSS_SELECTOR,"#hotsearch-content-wrapper > li")
# for i in elements :
#     print(i.text) #获取每个元素文本并输出
#     time.sleep(1)

# #根据cssSelector选择器查找
# element=driver.find_element(By.CSS_SELECTOR,"#hotsearch-content-wrapper > li:nth-child(3) > a > span.title-content-title")
# print(element.text)
# #根据xpath路径查找
# element=driver.find_element(By.XPATH,"//*[@id=\"hotsearch-content-wrapper\"]/li[3]/a/span[2]")
# print(element.text)

# # 找到百度输入框
# element=(driver.find_element(By.CSS_SELECTOR,"#kw"))
# # 输入关键词“牢大”
# element.send_keys("牢大")
# time.sleep(2)
# # 清空输入框
# element.clear()
# # 重新输入关键词“giegie”
# element.send_keys("giegie")
# # 获取“百度一下”按钮，并点击
# driver.find_element(By.CSS_SELECTOR,"#su").click()
# time.sleep(2)

# 查找元素，获取文本数据，并断言
# element=driver.find_element(By.CSS_SELECTOR,"#hotsearch-content-wrapper > li:nth-child(10) > a > span.title-content-title")
# text=element.text
# print(text)
# assert text=="“好房子”长这样"
#
# # 查找元素，获取元素属性(不是文本数据)
# element=driver.find_element(By.CSS_SELECTOR,"#su")
# value=element.get_attribute("value")
# print(value)
#
# # 获取当前页标题
# title=driver.title
# print(title)
#
# # 获取当前页URL
# cur_url=driver.current_url
# print(cur_url)

# # 获取元素并并点击跳转，输出跳转前后的title和URL
# before_title="before"+driver.title
# before_url="before"+driver.current_url
# print(before_title+"   "+before_url)
# time.sleep(2)
#
# element=driver.find_element(By.CSS_SELECTOR,"#s-top-left > a:nth-child(4)")
# element.click()
# # https://tieba.baidu.com/
# # driver.switch_to.window()
# 打开新的标签页需要切换句柄才能获取新标签页的标题和URL
# # 获取当前页面句柄
# cur_handle=driver.current_window_handle
# # 获取所有句柄
# handles=driver.window_handles
# # 切换句柄
# for handle in handles :
#     if handle!=cur_handle :
#         driver.switch_to.window(handle)
#         break
#
# after_title="after"+driver.title
# after_url="after"+driver.current_url
# print(after_title+"   "+after_url)
# time.sleep(2)

# 设置窗口为最大
# driver.maximize_window()
# #窗⼝最⼩化
# driver.minimize_window()
# #窗⼝全屏
# driver.fullscreen_window()
# #⼿动设置窗⼝⼤⼩
# driver.set_window_size(1024,768)
# 截取当前窗口并保存为“test.png”
# driver.save_screenshot("test.png") # 保存在当前路径下
# pathname="./images/" # 屏幕截图的保存位置
# # 获取当前时间并进行格式化转换作为截图名字
# filename="test-"+datetime.datetime.now().strftime("%Y-%m-%d_%H%M%S")+".png" # 截图的名字
# print(pathname+filename)
# driver.save_screenshot(pathname+filename)

# element=driver.find_element(By.CSS_SELECTOR,"#s-top-left > a:nth-child(4)")
# element.click()
# time.sleep(2)
# # 关闭默认标签页
# driver.close()
# time.sleep(2)

# # 打开一个警告弹窗页面
# driver.get("file:///D:/Desktop/selenium-html/selenium-html/alert.html#")
# time.sleep(2)
#
# # 获取弹窗元素并点击
# element=driver.find_element(By.CSS_SELECTOR,"#tooltip");
# element.click()
# time.sleep(2)
#
# # 获取弹窗
# alert=driver.switch_to.alert
# # 确认
# alert.accept()
# # 取消
# # alert.dismiss()
# time.sleep(2)

## 打开一个确认/取消弹窗，不同操作会有不同结果
# driver.get("file:///D:/Desktop/selenium-html/selenium-html/confirm.html")
# time.sleep(2)
# # 获取元素
# element=driver.find_element(By.CSS_SELECTOR,"body > input[type=button]")
# # 调起弹窗
# element.click()
# time.sleep(2)
#
# # 获取弹窗页面
# alert=driver.switch_to.alert
# # 确认
# # alert.accept()
# # 取消
# alert.dismiss()
# time.sleep(2)

# 打开一个输入框弹窗
# driver.get("file:///D:/Desktop/selenium-html/selenium-html/Prompt.html")
# time.sleep(1)
#
# # 获取元素
# element=driver.find_element(By.CSS_SELECTOR,"body > input[type=button]")
# # 调起弹窗
# element.click()
# time.sleep(1)
#
# # 获取弹窗页面
# alert=driver.switch_to.alert
#
# # 输入数据
# alert.send_keys("nihao")
# time.sleep(1)
#
# # 点击确认
# alert.accept()
# time.sleep(1)

# 关闭浏览器,关闭所有标签页
driver.quit()